package com.shxzz.easy12306.services.order.mq.consumer;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.shxzz.easy12306.framework.starter.common.enums.RefundType;
import com.shxzz.easy12306.framework.starter.common.mq.base.MessageWrapper;
import com.shxzz.easy12306.framework.starter.common.mq.constant.OrderRocketMQConstant;
import com.shxzz.easy12306.framework.starter.common.mq.event.RefundResultCallbackOrderEvent;
import com.shxzz.easy12306.framework.starter.idempotent.annotation.Idempotent;
import com.shxzz.easy12306.framework.starter.idempotent.enums.IdempotentKeyType;
import com.shxzz.easy12306.framework.starter.idempotent.enums.IdempotentScene;
import com.shxzz.easy12306.services.order.common.constant.RocketMQConstant;
import com.shxzz.easy12306.services.order.common.enums.OrderItemStatus;
import com.shxzz.easy12306.services.order.common.enums.OrderStatus;
import com.shxzz.easy12306.services.order.dao.entity.OrderItemDO;
import com.shxzz.easy12306.services.order.dto.domain.OrderItemStatusTransition;
import com.shxzz.easy12306.services.order.service.OrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author shxzz
 * @create 2024-03-17-20:25
 */
@Slf4j
@RocketMQMessageListener(
        topic = OrderRocketMQConstant.REFUND_CALLBACK_ORDER_TOPIC,
        selectorExpression = OrderRocketMQConstant.REFUND_CALLBACK_ORDER_TAG,
        consumerGroup = RocketMQConstant.REFUND_CALLBACK_ORDER_CG
)
@RequiredArgsConstructor
@Component
public class RefundCallbackConsumer implements RocketMQListener<MessageWrapper<RefundResultCallbackOrderEvent>> {

    private final OrderService orderService;

    @Idempotent(
            scene = IdempotentScene.MQ,
            type = IdempotentKeyType.SPEL,
            uniqueKeyPrefix = "easy12306-order:refund-callback:",
            spel = "#message.getKeys()",
            keyTimout = 7200L
    )
    @Override
    public void onMessage(MessageWrapper<RefundResultCallbackOrderEvent> message) {
        RefundResultCallbackOrderEvent refundResultCallbackOrderEvent = message.getMessage();
        List<OrderItemDO> orderItemDOList = refundResultCallbackOrderEvent.getRefundPassengerDetailList().stream()
                .map(refundPassengerDTO -> BeanUtil.toBean(refundPassengerDTO, OrderItemDO.class))
                .toList();
        log.info("退款信息消费，订单号：{},退款信息：{}", refundResultCallbackOrderEvent.getOrderSn(), JSON.toJSONString(refundResultCallbackOrderEvent.getRefundPassengerDetailList()));
        OrderItemStatusTransition orderItemStatusTransition = OrderItemStatusTransition.builder()
                .orderSn(refundResultCallbackOrderEvent.getOrderSn())
                .orderItemStatus(OrderItemStatus.REFUNDED)
                .orderItemDOList(orderItemDOList)
                .build();
        if(RefundType.FULL_REFUND.equals(refundResultCallbackOrderEvent.getRefundType())) {
            orderItemStatusTransition.setOrderStatus(OrderStatus.FULL_REFUND);
        } else {
            orderItemStatusTransition.setOrderStatus(OrderStatus.PARTIAL_REFUND);
        }
        orderService.refundCallback(orderItemStatusTransition);
    }
}
